//      folder_options.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "folder_options.h"

void _folder_options_add_existing_extensions ( GtkTreeStore * store, gchar * filename )
{
	GKeyFile * file = g_key_file_new () ;
	GError * error = null ;
	
	if ( ! g_key_file_load_from_file ( file, filename, G_KEY_FILE_NONE, & error ))
	{
		g_critical ( error -> message ) ;
		g_key_file_free ( file ) ;
		
		return ;
	}
	
	gchar ** g = g_key_file_get_groups ( file, null ) ;
	gint i = 0 ;
	
	while ( g [i] != null )
	{
		gchar * exec = g_key_file_get_string ( file, g [i], "exec", & error ) ;
		if ( error )
		{
			g_debug ( "[%s] %s", __func__, error -> message ) ;
			error = null ;
			
			g_free ( exec ) ;
			i ++ ;
			
			continue ;
		}
		
		GtkTreeIter iter ;
		gtk_tree_store_append ( store, & iter, null ) ;
		
		gtk_tree_store_set ( store, & iter, 0, g [i], 1, exec, -1 ) ;
		g_free ( exec ) ;
		
		i ++ ;
	}
	
	g_key_file_free ( file ) ;
	g_strfreev ( g ) ;
}

void _folder_options_save_mime_db ( GtkTreeStore * store )
{
	gint i = 0, len = gtk_tree_model_iter_n_children ( store, null ) ;
	GKeyFile * file = g_key_file_new () ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		GtkTreeIter iter ;
		if ( gtk_tree_model_iter_nth_child ( store, & iter, null, i ))
		{
			gchar * ext = null, * app = null ;
			gtk_tree_model_get ( store, & iter, 0, & ext, 1, & app, -1 ) ;
			
			g_key_file_set_string ( file, ext, "exec", app ) ;

			g_free ( app ) ;
			g_free ( ext ) ;
			
		}
	}
	
	gchar * db = g_build_filename ( g_get_home_dir (), ".system", "mime.db", null ) ;
	april_key_file_write ( db, file ) ;
	
	g_key_file_free ( file ) ;
	g_free ( db ) ;
}
	

vodi _folder_options_save_mime_information ( GtkCellRendererText * r, gchar * path, gchar * new_text, GtkTreeStore * store )
{
	GtkTreePath * pathx = gtk_tree_path_new_from_string ( path ) ;
	gint column = g_object_get_data ( r, "column" ) ;
	
	GtkTreeIter iter ;
	if ( gtk_tree_model_get_iter ( store, & iter, pathx ))
		gtk_tree_store_set ( store, & iter, column, new_text, -1 ) ;
	
	gtk_tree_path_free ( pathx ) ;
	
	gint i = 0, len = gtk_tree_model_iter_n_children ( store, null ) ;
	GKeyFile * file = g_key_file_new () ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		GtkTreeIter iter ;
		if ( gtk_tree_model_iter_nth_child ( store, & iter, null, i ))
		{
			gchar * ext = null, * app = null ;
			gtk_tree_model_get ( store, & iter, 0, & ext, 1, & app, -1 ) ;
			
			g_key_file_set_string ( file, ext, "exec", app ) ;

			g_free ( app ) ;
			g_free ( ext ) ;
			
		}
	}
	
	gchar * db = g_build_filename ( g_get_home_dir (), ".system", "mime.db", null ) ;
	april_key_file_write ( db, file ) ;
	
	g_key_file_free ( file ) ;
	g_free ( db ) ;
}

void _folder_options_mime_types_delete_row ( GtkTreeView * view )
{
	GtkTreeSelection * s = gtk_tree_view_get_selection ( view ) ;
	if ( gtk_tree_selection_count_selected_rows ( s ) == 0 )
		return ;
	
	GtkTreeStore * store = null ;
	GList * list = gtk_tree_selection_get_selected_rows ( s, & store ) ;
	
	gint i = 0, len = g_list_length ( list ) ;
	
	for ( i = 0 ; i < len ; i ++ )
	{
		GtkTreeIter iter ;
		if ( gtk_tree_model_get_iter ( store, & iter, g_list_nth_data ( list, i )))
			gtk_tree_store_remove ( store, & iter ) ;
	}
	
	
	g_list_foreach ( list, gtk_tree_path_free, null ) ;
	g_list_free ( list ) ;
	
	_folder_options_save_mime_db ( store ) ;
}
		
void _folder_options_mime_types_add_row ( GtkTreeView * view )
{
	GtkTreeIter iter ;
	GtkTreeStore * store = gtk_tree_view_get_model ( view ) ;
	
	gtk_tree_store_append ( store, & iter, null ) ;
	
	gtk_tree_store_set ( store, & iter, 0, ".ext", 1, "program", -1 ) ;
	gtk_tree_selection_select_iter ( gtk_tree_view_get_selection ( view ),
									 & iter ) ;
									 
	_folder_options_save_mime_db ( store ) ;
}

GtkWidget * folder_options_mime_types ( Explorer * e )
{
	gchar * db = g_build_filename ( g_get_home_dir (), ".system", "mime.db", null ) ;
	if ( ! g_file_test ( db, G_FILE_TEST_EXISTS ))
	{
		if ( g_file_test ( "/system/mime.db", G_FILE_TEST_EXISTS ))
			system ( "cp -v /system/mime.db ~/.system" ) ;
		else
		{
			g_free ( db ) ;
			
			g_warning ( "Cannot find mime database !" ) ;
			return null ;
		}
	}
	
	GtkTreeView * view = gtk_tree_view_new () ;
	GtkTreeStore * store = gtk_tree_store_new ( 2, G_TYPE_STRING, G_TYPE_STRING ) ;
	
	gtk_tree_view_set_model ( view, store ) ;
	
	GtkCellRenderer * r1 = gtk_cell_renderer_text_new (),
					* r2 = gtk_cell_renderer_text_new () ;
					
	GtkTreeViewColumn * col1 = gtk_tree_view_column_new_with_attributes ( "File type",
																		  r1,
																		"text",
																		0,
																		null ),
						* col2 = gtk_tree_view_column_new_with_attributes ( "Program",
																			r2,
																			"text",
																			1,
																			null ) ;
	
	gtk_tree_view_append_column ( view, col1 ) ;
	gtk_tree_view_append_column ( view, col2 ) ;
	
	g_object_set ( r1, "editable", true, null ) ;
	g_object_set ( r2, "editable", true, null ) ;
	
	g_object_set_data ( r1, "column", 0 ) ;
	g_object_set_data ( r2, "column", 1 ) ;
	
	g_signal_connect ( r1, "edited", _folder_options_save_mime_information, store ) ;
	g_signal_connect ( r2, "edited", _folder_options_save_mime_information, store ) ;
	
	
	GtkVBox * box = gtk_vbox_new ( no, 2 ) ;
	GtkScrolledWindow * sw = gtk_scrolled_window_new ( null, null ) ;
	
	gtk_box_pack_start ( box, sw, yes, yes, 5 ) ;
	gtk_container_add ( sw, view ) ;
	
	GtkBox * b = gtk_hbox_new ( no, 2 ) ;
	gtk_box_pack_start ( box, b, no, no, 2 ) ;
	
	GtkButton * add = gtk_button_new_with_label ( "Add new" ),
			  * del = gtk_button_new_with_label ( "Delete" ) ;
	
	_folder_options_add_existing_extensions ( store, db ) ;
	
	g_signal_connect_swapped ( add, "clicked", _folder_options_mime_types_add_row, view ) ;
	g_signal_connect_swapped ( del, "clicked", _folder_options_mime_types_delete_row, view ) ;
			  
	gtk_box_pack_start ( b, add, no, no, 2 ) ;
	gtk_box_pack_start ( b, del, no, no, 2 ) ;
	
	gtk_widget_show_all ( box ) ;
	g_free ( db ) ;
	
	return box ;
}


vodi folder_options ( Explorer * e )
{
	GtkDialog * dialog = gtk_dialog_new_with_buttons ( "Folder options",
														null,
														GTK_DIALOG_MODAL,
														"Close",
														1,
														null ) ;
	
	GtkNotebook * main = gtk_notebook_new () ;
	gtk_box_pack_start ( dialog -> vbox, main, yes, yes, 5 ) ;
	
	GtkTable * t1 = gtk_table_new ( 1, 1, no ) ;
	gtk_notebook_append_page ( main, t1, gtk_label_new ( "Explorer" )) ;
	
	GtkCheckButton * click = gtk_check_button_new () ;
	if ( e -> config.click_mode == 1 )
		gtk_toggle_button_set_active ( click, on ) ;
	
	gtk_table_attach ( t1, gtk_label_new ( "Single click to open items" ),
						0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 5, 5 ) ;
	gtk_table_attach ( t1, click, 2, 3, 0, 1, GTK_SHRINK, GTK_SHRINK, 5, 5 ) ;
	
	GtkWidget * file_types = folder_options_mime_types ( e ) ;
	if ( file_types )
		gtk_notebook_append_page ( main, file_types, gtk_label_new ( "File types" )) ;
	
	gtk_window_resize ( dialog, 400, 400 ) ;
	
	gtk_widget_show_all ( dialog ) ;
	gtk_dialog_run ( dialog ) ;
	
	if ( gtk_toggle_button_get_active ( click ))
		e -> config.click_mode = 1 ;
	else
		e -> config.click_mode = 2 ;
	
	gtk_widget_destroy ( dialog ) ;
}
